﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace L11
{
    public partial class Form1 : Form
    {
        private Bitmap bmBackground;//背景图片资源

        private Bitmap bmPaddle;//挡板图片资源
        private static readonly Rectangle GamePaddleRect
            = new Rectangle(39, 1, 93, 23);//挡板资源在合图中的矩形位置
        private Point centeofPaddle;//挡板中心位置

        private Bitmap bmBlock;
        private static readonly Rectangle GameBlockRect =
            new Rectangle(139, 1, 62, 27);
        private int gamelevel = 1;//游戏级别
        private Bitmap bmBall;
        private static readonly Rectangle GameBallRect = new Rectangle(1, 1, 36, 36);
        private Rectangle paddleRect;//挡板位置


        private void Form1_Load(object sender, EventArgs e)
        {
            this.LoadPictures();
            //设置挡板的初始位置
            centeofPaddle.X = this.ClientRectangle.Width / 2;
            centeofPaddle.Y = this.ClientRectangle.Height - 30;
        }
        //利用双缓存机制绘制游戏界面
        protected override void OnPaint(PaintEventArgs e)
        {
            //1.在内存中建立绘制区域
            Bitmap bufferBm = new Bitmap(
                this.ClientRectangle.Width - 1,
                this.ClientRectangle.Height - 1);
            Graphics g = Graphics.FromImage(bufferBm);

            //2.在内存中绘制所有图片资源
            this.DrawGame(g);
            //3.把内存中的区域一次性绘制在屏幕上
            e.Graphics.DrawImage(bufferBm, 0, 0);
            //4.释放当前内存区域
            g.Dispose();
            base.OnPaint(e);
        }

        /// <summary>
        /// 载入图片资源
        /// </summary>
        public static Bitmap LoadBm(string bmFilename)
        {
            return new Bitmap(Application.StartupPath + "\\GamePictures\\" + bmFilename + ".bmp");
        }
        private void LoadPictures()
        {
            bmBackground = BitmapHelper.LoadBm("Background");
            Bitmap bmBreak = BitmapHelper.LoadBm("Break");

            bmPaddle = bmBreak.Clone(GamePaddleRect, bmBreak.PixelFormat);

            bmBlock = bmBreak.Clone(GameBlockRect, bmBreak.PixelFormat);

            bmBall = bmBreak.Clone(GameBallRect, bmBreak.PixelFormat);
        }
        /// <summary>
        /// 游戏界面绘制
        /// </summary>
        /// <param name="g"></param>
        private void DrawGame(Graphics g)
        {
            //绘制游戏信息
            g.DrawString("关卡" + gamelevel.ToString(),
                new Font("仿宋", 24),
                new SolidBrush(Color.White),
                new Point(700, 5));
        }

        /// <summary>
        /// 处理鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBlock_MouseMove(object sender, MouseEventArgs e)
        {
            //移动挡板 改变挡板的中心坐标
            if (e.X <= 93 / 2)
            {
                centeofPaddle.X = 93 / 2;
            }
            else if (e.X >= this.ClientRectangle.Width - 93 / 2)
            {
                centeofPaddle.X = this.ClientRectangle.Width - 93 / 2;
            }
            else
            {
                centeofPaddle.X = e.X;
            }
            //更新挡板位置
            paddleRect.X = centeofPaddle.X - 48;


            //this.Invalidate();

        }
    }
    public class BitmapHelper
    {
        /// <summary>
        /// 载入图片资源
        /// </summary>
        /// <param name="bmFilename">图片文件名</param>
        /// <returns>位图（Bitmap）资源</returns>
        public static Bitmap LoadBm(string bmFilename)
        {
            return new Bitmap(Application.StartupPath + "\\GamePictures\\" + bmFilename + ".bmp");
        }
    }
}




